package com.cc8w.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 封装页面请求过来的数据格式
 * @author Administrator
 *
 */

public class RequestData extends HashMap<String, Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public RequestData() {
		
	}
	public RequestData(HttpServletRequest req) throws IOException, DocumentException{
	
		/*
		  
		//请求头信息
		Enumeration<String> ss = req.getHeaderNames();
		System.out.println(ss);
		do{
			//System.out.println(ss.nextElement());
			String header_str = req.getHeader(ss.nextElement());
			System.out.println(ss.nextElement()+"----"+header_str);
			
		}while(ss.hasMoreElements());
		
		*/
		
		//如果是普通表单,那么就进行解析
		if(!ServletFileUpload.isMultipartContent(req)) {
			
			//请求参数key=value	
			Map<String, String[]> req_m = req.getParameterMap();
			
			//遍历map
			req_m.forEach((key,value)->{
				//System.out.println(key + "：" );
				String k = key;
				String [] v = value;
				
//				if(v.length>1) {				
//					this.put(k,v);
//				}else {
//					this.put(k,v[0]);
//				}
				this.put(k,v);
				
			});
			
			//测试打印一下接收数据
			String req_data = JSON.toJSONString(this);
			System.out.println(req_data);

		}else { //表单中有文件上传,留给其他程序调用
			
		}
		

		

		
	
	
        
       
       
		
		
		
		
	}
	/**
	 * 如果请求头里面有xml数据这样解析
	 * 前提:要引入 dom4j 库
	 */
	
	public void analysisXml(HttpServletRequest req) throws IOException, DocumentException{
		//如果提交xml数据
		
		// 获取HTTP请求的输入流
        // 已HTTP请求输入流建立一个BufferedReader对象
        BufferedReader br = new BufferedReader(new InputStreamReader(
        		req.getInputStream(), "UTF-8"));

        String buffer = null;
        // 存放请求内容
        StringBuffer xml = new StringBuffer();
        while ((buffer = br.readLine()) != null) {
            // 在页面中显示读取到的请求参数
            xml.append(buffer);
        }
        Document document = null;
        SAXReader reader = new SAXReader();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(xml
                .toString().getBytes());

        InputStreamReader ir = new InputStreamReader(inputStream);
		//document已经获取到xml文件
		document = reader.read(ir);
		//根据项目需求，解析xml文件
		
		// 获取根节点 emps
		Element root = document.getRootElement();
		// 查找指定节点名称QName的所有子节点elements
		List<Element> list = root.elements("emp");  //得到所有emp元素
		System.out.println("list==>"+list);
		
		for (Element element : list) { //循环输出全部emp的相关信息  
			System.out.println("element.getName()==>"+element.getName());
			
			List<Element> list2 = element.elements(); //得到emp元素下的子元素
			
			for (Element e : list2) {  //遍历emp元素下的子元素
				System.out.print(e.getName()+":");  //获取子元素名称
				System.out.print(e.getText()+" ");  //获取子元素的文本值
			}
			 System.out.println();
		}
		
		
	}
	
	
	public String getString(String key) {
		String[] obj = (String[]) this.get(key);
		if(obj instanceof String[]);
			String value = (String) obj[0];
		return value;
	}
	
	public String[] getStrArr(String key) {
		Object obj = this.get(key);
		if(obj instanceof String[]);
			String[] value = (String[]) obj;
		return value;
	}
	public int getInt(String key) {
		
		@SuppressWarnings("null")
		int value=(Integer) null;
		String[] obj = (String[]) this.get(key);

		System.out.println(obj);
		if(obj instanceof String[] ) {
			value = Integer.parseInt((String)obj[0]);
		}
		    
		return value;
		
	}
	
	


}
